# 对数据做一阶差分
# 滑动窗口，计算相邻两个窗口之间的相似性，度量指标DTW
# 对转换后的时间序列做平滑

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from fastdtw import fastdtw
from scipy.spatial.distance import euclidean
import time
time_start = time.clock()
selected = ['1_executor_cpuTime_count', '1_executor_shuffleLocalBytesRead_count', '1_executor_shuffleRecordsRead_count', '1_executor_shuffleRemoteBytesRead_count', '1_executor_shuffleTotalBytesRead_count', '1_jvm_heap_committed_value', '1_jvm_heap_usage_value', '1_jvm_heap_used_value', '1_jvm_pools_PS-Eden-Space_max_value', '1_jvm_pools_PS-Eden-Space_usage_value', '1_jvm_pools_PS-Eden-Space_used_value', '1_jvm_pools_PS-Old-Gen_used_value', '1_jvm_pools_PS-Survivor-Space_committed_value', '1_jvm_pools_PS-Survivor-Space_max_value', '1_jvm_pools_PS-Survivor-Space_usage_value', '1_jvm_pools_PS-Survivor-Space_used_value', '1_jvm_total_committed_value', '1_jvm_total_used_value', '2_jvm_heap_usage_value', '2_jvm_heap_used_value', '2_jvm_pools_PS-Eden-Space_usage_value', '2_jvm_pools_PS-Eden-Space_used_value', '2_jvm_pools_PS-Survivor-Space_committed_value', '2_jvm_pools_PS-Survivor-Space_max_value', '2_jvm_pools_PS-Survivor-Space_used_value', '2_jvm_total_used_value', 'driver_BlockManager_memory_memUsed_MB_value', 'driver_BlockManager_memory_onHeapMemUsed_MB_value', 'driver_BlockManager_memory_remainingMem_MB_value', 'driver_BlockManager_memory_remainingOnHeapMem_MB_value']


# data = pd.read_csv('./结果统计/bursty_input/4_1_100000_61/exe1_add_4/4_1_100000_61_exe4.csv')
data = pd.read_csv('../exathlon-master/data/raw/app3/3_2_1000000_71.csv')
data.set_index(['t'], inplace=True)
data = data.diff()
data.drop(data.head(11).index, inplace=True)  # 从头去掉n行
data.drop(data.tail(10).index, inplace=True)  # 从尾部去掉n行
# 去掉重复索引的行
data = data.loc[~data.index.duplicated(keep='first')]

slide_dtw = pd.DataFrame()
columns = selected
for n in range(len(columns)):
    print(n)
    metric = columns[n]
    m = data[metric]
    record_dtw = []
    for i in range(0, len(m)-200-100, 100):
        s1 = np.array(m[i:i+200].tolist())
        s2 = np.array(m[i+100: i+100+200].tolist())
        # current = dtw(s1, s2)
        distance, path = fastdtw(s1, s2, dist=euclidean)
        record_dtw.append(distance)
    slide_dtw[metric] = record_dtw
# slide_dtw.to_csv('./结果统计/bursty_input/4_1_100000_61/exe1_add_4/4_1_100000_61_exe4_dtw.csv')


simp_moving_avg = slide_dtw.rolling(window=10, min_periods=1).mean()
# simp_moving_avg.to_csv('./结果统计/bursty_input/4_1_100000_61/exe1_add_4/4_1_100000_61_exe4_dtw_smooth.csv')

time_end = time.clock()
time_sum = time_end - time_start
print(time_sum)




